package ecom.shell;

import java.io.PrintStream;

import shell.command.ShellCommandImpl;
import shell.command.ShellContext;
import ecom.ejb.EuroConverterRemote.Devise;

/**
 * eCommerce Application Sample for J2EE Training. Changement de la devise.
 * 
 * @version 1.0
 */
public final class CurrencyCommandImpl extends ShellCommandImpl implements
	EcomShellConstantes {
    /**
     * Constructeur.
     * 
     * @param context
     *            Le contexte
     */
    public CurrencyCommandImpl(final ShellContext context) {
	super(context);
    }

    /**
     * @see shell.command.ShellCommand#getName()
     */
    @Override
    public String getName() {
	return "currency";
    }

    /**
     * @see shell.command.ShellCommand#getUsage()
     */
    @Override
    public String getUsage() {
	return "currency [EUR|FRF|DEM|USD|Devise...]";
    }

    /**
     * @see shell.command.ShellCommand#getShortDescription()
     */
    @Override
    public String getShortDescription() {
	return "Sets currency; default is EUR";
    }

    /**
     * @see shell.command.ShellCommandImpl#execute(java.lang.String,
     *      java.io.PrintStream, java.io.PrintStream)
     */
    @Override
    public void execute(final String cmdline, final PrintStream out,
	    final PrintStream err) {
	if (cmdline.isEmpty()) {
	    out.println("La monnaie de sortie est "
		    + super.getContexte().getVar(
			    EcomShellConstantes.CURRENT_CURRENCY));
	} else {
	    try {
		getContexte().setVar(EcomShellConstantes.CURRENT_CURRENCY,
			Devise.valueOf(cmdline));
	    } catch (final IllegalArgumentException e) {
		out.println("Devise inconnue");
	    }
	}
    }
}
